import 'dart:io';
import 'package:url_launcher/url_launcher.dart';
import 'dart:math' as math;

import 'toast_util.dart';

class MapUtils {

  static goMap(dynamic adr, dynamic lat, dynamic lng) async{
    if(await gotoAMap(adr,lat,lng)){
      return;
    }else if(await gotoBaiduMap(adr,lat,lng)){
      return;
    }else if(await gotoTencentMap(adr,lat,lng)){
      return;
    }else{
      if(Platform.isAndroid){
        ToastUtil.toast('请安装地图',);
        return;
      }else{
        _gotoAppleMap(lat,lng);
        return;
      }
    }
  }

  /// 高德地图
  static Future<bool> gotoAMap(String adr, dynamic lat, dynamic lng) async {
    //安卓  amapuri://route/plan/?sid=BGVIS1&slat=39.92848272&slon=116.39560823&sname=A&did=&dlat=39.98848272&dlon=116.47560823&dname=B&dev=0&t=0
    //ios  iosamap://path?sourceApplication=applicationName&sid=&slat=39.92848272&slon=116.39560823&sname=A&did=&dlat=39.98848272&dlon=116.47560823&dname=B&dev=0&t=0
//    var url = '${Platform.isAndroid ? 'android' : 'ios'}amap://navi?sourceApplication=amap&lat=$latitude&lon=$longitude&dev=0&style=2';
    var url='';
    if(Platform.isAndroid){
      url='amapuri://route/plan/?sid=BGVIS1&slat=&slon=&sname=&did=&dlat=$lat&dlon=$lng&dname=$adr&dev=0&t=0';
    }else{
      url='iosamap://path?sourceApplication=applicationName&sid=BGVIS1&slat=&slon=&sname=&did=&dlat=$lat&dlon=$lng&dname=$adr&dev=0&t=0';
    }
    bool canLaunchUrl = await canLaunch(url);

    if (!canLaunchUrl) {
//      ToastUtils.toast(StringUtils.notchecktheamap,);
      return false;
    }

    await launch(url);

    return true;
  }

  /// 腾讯地图
  static Future<bool> gotoTencentMap(String adr, dynamic lat, dynamic lng) async {
    //var url = 'qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&tocoord=$latitude,$longitude&referer=IXHBZ-QIZE4-ZQ6UP-DJYEO-HC2K2-EZBXJ';
    var url = 'qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&to=$adr&tocoord=$lat,$lng&referer=IXHBZ-QIZE4-ZQ6UP-DJYEO-HC2K2-EZBXJ';
    bool canLaunchUrl = await canLaunch(url);

    if (!canLaunchUrl) {
//      ToastUtils.toast(StringUtils.notcheckthetencentmap,);
      return false;
    }

    await launch(url);

    return canLaunchUrl;
  }

  /// 百度地图
  static Future<bool> gotoBaiduMap(String adr, dynamic lat, dynamic lng) async {
//    var url = 'baidumap://map/direction?destination=$latitude,$longitude&coord_type=bd09ll&mode=driving';
    var url ='';
    if((lat!=null&&lat!='')&&(lng!=null&&lng!='')){
      url = 'baidumap://map/direction?region=beijing&origin=&destination=$lat,$lng&coord_type=bd09ll&mode=driving&src=${Platform.isAndroid?'andr':'ios'}.baidu.openAPIdemo';
    }else{
      url = 'baidumap://map/direction?region=beijing&origin=&destination=$adr&coord_type=bd09ll&mode=driving&src=${Platform.isAndroid?'andr':'ios'}.baidu.openAPIdemo';
    }
    bool canLaunchUrl = await canLaunch(url);

    if (!canLaunchUrl) {
//      ToastUtils.toast(StringUtils.notcheckthebdmap,);
      return false;
    }

    await launch(url);

    return canLaunchUrl;
  }

  /// 苹果地图
  static Future<bool> _gotoAppleMap(dynamic lat, dynamic lng) async {
    var url = 'http://maps.apple.com/?daddr=$lat,$lng';

    bool canLaunchUrl = await canLaunch(url);

    if (!canLaunchUrl) {
      ToastUtil.toast('打开地图失败',);
      return false;
    }

    bool launched = await launch(url);
    return launched;
  }

  // 获取两个经纬度之间的距离 km
  static getetDistance(lat1, lng1, lat2, lng2) {
    print('getetDistance-->lat1=$lat1,lng1=$lng1,lat2=$lat2,lng2=$lng2,');
    var radLat1 = (lat1 * math.pi) / 180.0;
    var radLat2 = (lat2 * math.pi) / 180.0;
    var a = radLat1 - radLat2;
    var b = (lng1 * math.pi) / 180.0 - (lng2 * math.pi) / 180.0;
    var s =
        2 *
            math.asin(
                math.sqrt(
                    math.pow(math.sin(a / 2), 2) +
                        math.cos(radLat1) * math.cos(radLat2) * math.pow(math.sin(b / 2), 2)
                )
            );
    s = s * 6378.137; //地球半径

    return ((s * 10000) / 10000).toStringAsFixed(2);
  }
}